<!-- 专注力评测问卷编辑弹窗 -->
<template>
  <el-dialog
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate?'修改专注力评测问卷':'添加专注力评测问卷'"
    :visible="visible"
    width="550px"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="问题:"
        prop="question">
        <el-input
          v-model="form.question"
          :maxlength="20"
          clearable
          placeholder="请输入问题"/>
      </el-form-item>

      <el-form-item
        v-for="(option, index) in form.format_options"
        :key="index"
        :label="'选项' + parseInt(index + 1) + '：'"
        :prop="'format_options.' + index + '.label'"
        :rules="{
          required: true, message: '选项内容不能为空', trigger: 'blur'
        }">
        <el-row>
          <el-col :span="13">
            <el-form-item
              :prop="'format_options.' + index + '.label'"
              :rules="{
                required: true, message: '选项内容不能为空', trigger: 'blur'
              }">
              <el-input
                v-model="option.label"
                :maxlength="20"
                clearable
                placeholder="请输入选项"/>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              :prop="'format_options.' + index + '.value'"
              :rules="{
                required: true, message: '选项分值不能为空', trigger: 'blur'
              }">
                <el-input pattern="[0-9]*" @input="handleInput(index, $event)"   placeholder="请输入分数" v-model="option.value" style="margin-left: 5px">
                  <template slot="append">分</template>
                </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button style="margin-left: 10px" type="danger" size="mini" @click.prevent="removeOption(option)">删除</el-button>
          </el-col>
        </el-row>
        <el-button v-if="form.format_options.length == 0 || index+1 == form.format_options.length" type="primary" @click.prevent="addOption" size="mini">添加选项</el-button>
      </el-form-item>

<!--      <el-form-item-->
<!--        label="选项:"-->
<!--        :prop="'domains.' + index + '.value'">-->
<!--        <el-button style="margin-bottom: 5px" type="primary" @click.prevent="addOption" size="mini">添加选项</el-button>-->
<!--        <el-row v-for="(option, index) in form.format_options" :key="index">-->
<!--          <el-col :span="13">-->
<!--            <el-input-->
<!--              style="margin-bottom: 2px"-->
<!--              v-model="option.label"-->
<!--              :maxlength="20"-->
<!--              clearable-->
<!--              placeholder="请输入问题"/>-->
<!--          </el-col>-->
<!--          <el-col :span="7">-->
<!--            <el-input placeholder="请输入内容" v-model="option.value" style="margin-left: 5px">-->
<!--              <template slot="append">分</template>-->
<!--            </el-input>-->
<!--          </el-col>-->
<!--          <el-col :span="3">-->
<!--            <el-button style="margin-left: 10px" type="danger" size="mini" @click.prevent="removeOption(option)">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--      </el-form-item>-->

      <el-form-item label="问题类型:" prop="question_type">
        <el-select
          v-model="form.question_type"
          class="ele-block"
          clearable
          placeholder="请选择问题类型">
          <el-option v-for="item in factors" :label="item.title" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="备注:">
        <el-input
          type="textarea"
          rows="5"
          v-model="form.remark"
          clearable
          placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'AttentionQuestionnaireEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        question: [
          {required: true, message: '请输入问题', trigger: 'blur'}
        ],
        options: [
          {required: true, message: '请输入选项', trigger: 'blur'}
        ],

        question_type: [
          {required: true, message: '请选择问题类型', trigger: 'blur'}
        ],
        remark: [
          {required: false, message: '请输入备注', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      factors: []
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    this.getAttentionFactors();
  },
  methods: {
    getAttentionFactors() {
      this.$http.get('/attentionfactor/index').then(res => {
        if (res.data.code === 0) {
          this.factors = res.data.data;
          console.log(this.factors)
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    handleInput(index, value) {
      // 去除输入值中的非数字字符
      this.form.format_options[index].value = value.replace(/[^\d]/g, '');
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.options = JSON.stringify(this.form.format_options)
          this.$http.post('/attentionquestionnaire/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    removeOption(item) {
      var index = this.form.format_options.indexOf(item)
      if (index !== -1) {
        this.form.format_options.splice(index, 1)
      }
    },
    addOption() {
      this.form.format_options.push({
        label: '',
        value: ''
      });
    }
  }
}
</script>

<style scoped>
</style>
